<template> 
	<div class="app-container">
		<el-card class="operate-container" shadow="never">
			<i class="el-icon-tickets"></i>
			<span>数据列表</span>
			<el-button class="btn-add" @click="addRole()" size="mini">
				更新版本
			</el-button>
		</el-card>
		<div class="table-container">
			<el-table ref="roleTable" :data="list" style="width: 100%" @selection-change="handleSelectionChange" v-loading="listLoading"
			 border>

				<el-table-column type="selection" width="60" align="center"></el-table-column>
				<el-table-column label="编号" width="180" align="center">
					<template slot-scope="scope">{{scope.row.id}}</template>
				</el-table-column>
				<el-table-column label="类型" width="180" align="center">
					<template slot-scope="scope">{{scope.row.status | formatTime}}</template>
				</el-table-column>
				<el-table-column label="版本号" align="center">
					<template slot-scope="scope">{{scope.row.version}}</template>
				</el-table-column>
				<el-table-column label="更新内容" width="180" align="center">
					<template slot-scope="scope">{{scope.row.remark || '无'}}</template>
				</el-table-column>

				<el-table-column label="安装包下载地址" width="280" align="center">
					<template slot-scope="scope">{{scope.row.appUrl || scope.row.wgetUrl}}</template>
				</el-table-column>

				<!-- <el-table-column label="操作" align="center">
					<template slot-scope="scope">
						<el-button size="mini" @click="handleUpdate(scope.$index, scope.row)">编辑
						</el-button>
						<el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除
						</el-button>
					</template>
				</el-table-column> -->
			</el-table>
		</div>
		<!-- <div class="batch-operate-container">
			<el-select size="small" v-model="operateType" placeholder="批量操作">
				<el-option v-for="item in operates" :key="item.value" :label="item.label" :value="item.value">
				</el-option>
			</el-select>
			<el-button style="margin-left: 20px" class="search-button" @click="handleBatchOperate()" type="primary" size="small">
				确定
			</el-button>
		</div> -->
		<div class="pagination-container">
			<el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" layout="total, sizes,prev, pager, next,jumper"
			 :page-size="listQuery.pageSize" :page-sizes="[10,15,50]" :current-page.sync="listQuery.pageNum" :total="total">
			</el-pagination>
		</div>
	</div>
</template>
<script>
	import {
		formatDate
	} from '@/utils/date';
	import {
		getVersion
	} from '@/api/role'

	export default {
		name: 'roleList',
		data() {
			return {
				operates: [{
						label: "显示会员",
						value: "showRole"
					},
					{
						label: "隐藏会员",
						value: "hideRole"
					}
				],
				operateType: null,
				listQuery: {
					pageNum: 1,
					pageSize: 10
				},
				list: null,
				total: null,
				listLoading: true,
				multipleSelection: []
			}
		},
		created() {
			this.getList();
		},
		filters: {
			formatTime(status) {
				if (status == 0) {
					return '热更新'
				} else if (status == 1) {
					return '整包更新'
				}
			},
			formatStatus(status) {
				for (let i = 0; i < defaultStatusOptions.length; i++) {
					if (status === defaultStatusOptions[i].value) {
						return defaultStatusOptions[i].label;
					}
				}
			},
			formatReturnAmount(row) {
				return row.productRealPrice * row.productCount;
			}
		},
		methods: {
			getList() {
				this.listLoading = true;
				getVersion(this.listQuery).then(response => {
					this.listLoading = false;
					console.log('版本列表', response)
					this.list = response.data.records;
					this.total = response.data.total;
					//this.total = response.data.records.length;
					this.totalPage = response.data.pages;
					this.pageSize = response.data.size;
				});
			},
			handleSelectionChange(val) {
				this.multipleSelection = val;
			},
			handleUpdate(index, row) {
				this.$router.push({
					path: '/sys/updateRole',
					query: {
						id: row.id
					}
				})
			},
			handleDelete(index, row) {
				this.$confirm('是否要删除该会员', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					deleteRole(row.id).then(response => {
						this.$message({
							message: '删除成功',
							type: 'success',
							duration: 1000
						});
						this.getList();
					});
				});
			},
			getProductList(index, row) {
				console.log(index, row);
			},
			getProductCommentList(index, row) {
				console.log(index, row);
			},
			handleFactoryStatusChange(index, row) {
				var data = new URLSearchParams();
				data.append("ids", row.id);
				data.append("factoryStatus", row.factoryStatus);
				updateFactoryStatus(data).then(response => {
					this.$message({
						message: '修改成功',
						type: 'success',
						duration: 1000
					});
				}).catch(error => {
					if (row.factoryStatus === 0) {
						row.factoryStatus = 1;
					} else {
						row.factoryStatus = 0;
					}
				});
			},
			handleShowStatusChange(index, row) {
				let data = new URLSearchParams();

				data.append("ids", row.id);
				data.append("showStatus", row.status);
				updateShowStatus(data).then(response => {
					this.$message({
						message: '修改成功',
						type: 'success',
						duration: 1000
					});
				}).catch(error => {
					if (row.showStatus === 0) {
						row.showStatus = 1;
					} else {
						row.showStatus = 0;
					}
				});
			},
			handleSizeChange(val) {
				this.listQuery.pageNum = 1;
				this.listQuery.pageSize = val;
				this.getList();
			},
			handleCurrentChange(val) {
				this.listQuery.pageNum = val;
				this.getList();
			},
			searchRoleList() {
				this.listQuery.pageNum = 1;
				this.getList();
			},
			handleBatchOperate() {
				console.log(this.multipleSelection);
				if (this.multipleSelection < 1) {
					this.$message({
						message: '请选择一条记录',
						type: 'warning',
						duration: 1000
					});
					return;
				}
				let showStatus = 0;
				if (this.operateType === 'showRole') {
					showStatus = 1;
				} else if (this.operateType === 'hideRole') {
					showStatus = 0;
				} else {
					this.$message({
						message: '请选择批量操作类型',
						type: 'warning',
						duration: 1000
					});
					return;
				}
				let ids = [];
				for (let i = 0; i < this.multipleSelection.length; i++) {
					ids.push(this.multipleSelection[i].id);
				}
				let data = new URLSearchParams();
				data.append("ids", ids);
				data.append("showStatus", showStatus);
				updateShowStatus(data).then(response => {
					this.getList();
					this.$message({
						message: '修改成功',
						type: 'success',
						duration: 1000
					});
				});
			},
			addRole() {
				// this.$router.push({
				// 	path: '/sys/addVersion'
				// })
				this.$message({
					message: '数据库修改版本更新，无需管理员操作',
					type: 'success',
					duration: 3000
				});
			}
		}
	}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>


</style>
